from django.db import models
from django.contrib.contenttypes.models import ContentType  # 导入contentType
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation

from luckin.common import (
    goods_avatar_path,
    activity_tag_path,
    user_avatar_path,
    verify_code_picture_path
)


# Create your models here.

# 商品表
class Goods(models.Model):
    name = models.CharField(max_length=32, verbose_name="商品昵称")
    en_name = models.CharField(max_length=64, verbose_name="商品昵称(英文)", null=True)
    goods_details = models.TextField(verbose_name="商品介绍详情", null=True)
    avatar_path = models.ImageField(verbose_name="商品图片存放地址", upload_to=goods_avatar_path, null=True, blank=False)
    true_or_false_choices = [
        (1, "是"),
        (2, "否")
    ]
    sentiment_top = models.IntegerField(verbose_name="是否为人气Top", default=2, null=True, choices=true_or_false_choices)
    is_new = models.IntegerField(verbose_name="是否为新品", default=1, null=True, choices=true_or_false_choices)
    activity_tag = models.ImageField(upload_to=activity_tag_path, null=True)
    sales_state = models.IntegerField(choices=true_or_false_choices, verbose_name="是否售空", default=2, null=True)

    # 一对多关系
    comments = models.ForeignKey(to="Comments", to_field="id", on_delete=models.CASCADE, null=True)
    series = models.ForeignKey(to="Series", to_field="id", on_delete=models.DO_NOTHING,
                               verbose_name="系列", null=True)

    specification = GenericRelation("Specification", object_id_field="object_id", content_type_field="contentType")
    temperature = GenericRelation("Temperature", object_id_field="object_id", content_type_field="contentType")
    sugar = GenericRelation("Sugar", object_id_field="object_id", content_type_field="contentType")
    milk = GenericRelation("Milk", object_id_field="object_id", content_type_field="contentType")
    cream = GenericRelation("Cream", object_id_field="object_id", content_type_field="contentType")

    class Meta:
        db_table = "goods"
        verbose_name = "商品表"

    def __str__(self):
        return self.name


# 评论表
class Comments(models.Model):
    nickname = models.CharField(max_length=32, verbose_name="评论者", null=False)
    content = models.TextField(verbose_name="评论内容", null=False)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="评论时间")

    class Meta:
        db_table = "comments"
        verbose_name = "大咖说表"

    def __str__(self):
        return self.nickname


class Specification(models.Model):
    # 商品规格，大杯，小杯，中杯
    choices = [
        (1, "大杯"),
        (2, "中杯"),
        (3, "小杯"),
    ]
    name = models.IntegerField(choices=choices, verbose_name="商品规格昵称")
    price = models.DecimalField(verbose_name="商品原价格", default=0.00, decimal_places=2, max_digits=5)
    salt_price = models.DecimalField(verbose_name="商品销售价格", default=0.00, decimal_places=2, max_digits=5)
    default_choices = [
        (1, "是"),
        (2, "否"),
    ]
    # 是否默认选中，默认为否
    is_default = models.IntegerField(choices=default_choices, default=2)

    contentType = models.ForeignKey(to=ContentType, on_delete=models.DO_NOTHING)
    object_id = models.PositiveIntegerField()
    content_obj = GenericForeignKey("contentType", "object_id")

    class Meta:
        db_table = "specification"
        verbose_name = "商品规格表"

    def __str__(self):
        return "%s-%s" % (self.content_obj.name, self.get_name_display())


class Temperature(models.Model):
    # 商品温度，常温，冰，热
    choices = [
        (1, "常温"),
        (2, "冰"),
        (3, "热"),
    ]
    name = models.IntegerField(choices=choices, verbose_name="商品温度昵称")
    default_choices = [
        (1, "是"),
        (2, "否"),
    ]
    # 是否默认选中，默认为否
    is_default = models.IntegerField(choices=default_choices, default=2)

    contentType = models.ForeignKey(to=ContentType, on_delete=models.DO_NOTHING)
    object_id = models.PositiveIntegerField()
    content_obj = GenericForeignKey("contentType", "object_id")

    class Meta:
        db_table = "temperature"
        verbose_name = "商品温度表"

    def __str__(self):
        return "%s-%s" % (self.content_obj.name, self.get_name_display())


class Sugar(models.Model):
    # 商品糖度，无糖，半份糖，单份糖，香草3元，榛子，全糖(推荐)
    choices = [
        (1, "无糖"),
        (2, "半份糖"),
        (3, "单份糖"),
        (4, "香草3元"),
        (5, "榛子"),
        (6, "全糖(推荐)")
    ]
    name = models.IntegerField(choices=choices, verbose_name="商品糖度昵称")
    price = models.DecimalField(max_digits=5, decimal_places=2, verbose_name="加价", default=0)
    default_choices = [
        (1, "是"),
        (2, "否"),
    ]
    # 是否默认选中，默认为否
    is_default = models.IntegerField(choices=default_choices, default=2)

    contentType = models.ForeignKey(to=ContentType, on_delete=models.DO_NOTHING)
    object_id = models.PositiveIntegerField()
    content_obj = GenericForeignKey("contentType", "object_id")

    class Meta:
        db_table = "sugar"
        verbose_name = "商品糖度表"

    def __str__(self):
        return "%s-%s" % (self.content_obj.name, self.get_name_display())


class Milk(models.Model):
    # 商品奶度,无奶,单份奶,双份奶
    choices = [
        (1, "无奶"),
        (2, "单份奶"),
        (3, "双份奶"),
    ]
    name = models.IntegerField(choices=choices, verbose_name="商品奶度昵称")
    default_choices = [
        (1, "是"),
        (2, "否"),
    ]
    # 是否默认选中，默认为否
    is_default = models.IntegerField(choices=default_choices, default=2)

    contentType = models.ForeignKey(to=ContentType, on_delete=models.DO_NOTHING)
    object_id = models.PositiveIntegerField()
    content_obj = GenericForeignKey("contentType", "object_id")

    class Meta:
        db_table = "milk"
        verbose_name = "商品奶度表"

    def __str__(self):
        return "%s-%s" % (self.content_obj.name, self.get_name_display())


class Cream(models.Model):
    # 商品奶油度，默认奶油，无奶油
    choices = [
        (1, "默认奶油"),
        (2, "无奶油"),
        (3, "加奶油3元"),
    ]
    name = models.IntegerField(choices=choices, verbose_name="商品奶油度昵称")
    price = models.DecimalField(max_digits=5, decimal_places=2, verbose_name="加价", default=0)
    default_choices = [
        (1, "是"),
        (2, "否"),
    ]
    # 是否默认选中，默认为否
    is_default = models.IntegerField(choices=default_choices, default=2)

    contentType = models.ForeignKey(to=ContentType, on_delete=models.DO_NOTHING)
    object_id = models.PositiveIntegerField()
    content_obj = GenericForeignKey("contentType", "object_id")

    class Meta:
        db_table = "cream"
        verbose_name = "商品奶油度表"

    def __str__(self):
        return "%s- %s" % (self.content_obj.name, self.get_name_display())


# 分类 => 系列 => 商品

class Series(models.Model):
    # 商品系列表
    name = models.CharField(max_length=32, unique=True, null=False, verbose_name="商品系列昵称")
    category = models.ForeignKey(to="Category", to_field="id", on_delete=models.DO_NOTHING, verbose_name="分类")

    class Meta:
        db_table = "series"
        verbose_name = "商品系列表"

    def __str__(self):
        return self.name


class Category(models.Model):
    # 商品分类
    name = models.CharField(max_length=32, unique=True, null=False, verbose_name="商品分类昵称")

    class Meta:
        db_table = "category"
        verbose_name = "商品分类表"

    def __str__(self):
        return self.name


# 用户数据表
class User(models.Model):
    nickname = models.CharField(max_length=64, null=False, verbose_name="用户名")
    gender_choices = [
        (1, "男"),
        (2, "女"),
        (3, "其他")
    ]
    gender = models.IntegerField(choices=gender_choices, verbose_name="性别", default=1)
    phone = models.CharField(max_length=11, verbose_name="手机号", null=False, unique=True, db_index=True)
    avatar = models.ImageField(verbose_name="用户头像", upload_to=user_avatar_path, null=True, default="user/default.jpg")
    password = models.CharField(max_length=32, verbose_name="密码", null=True)

    class Meta:
        db_table = "user"
        verbose_name = "用户表"

    def __str__(self):
        return self.nickname + "-" + self.phone


# 收货地址
class ShippingAddress(models.Model):
    user = models.ForeignKey("User", to_field="id", on_delete=models.DO_NOTHING)
    nickname = models.CharField(max_length=64, verbose_name="收获联系人", null=False)
    gender_choices = [
        (1, "先生"),
        (2, "女士"),
    ]
    gender = models.IntegerField(choices=gender_choices, verbose_name="性别", null=False)
    phone = models.CharField(max_length=11, verbose_name="联系方式", null=False)
    address = models.CharField(max_length=255, verbose_name="收货地址")
    home_number = models.CharField(max_length=64, null=True, verbose_name="门牌号")
    address_tag_choices = [
        (1, "家"),
        (2, "公司"),
        (3, "学校"),
    ]
    address_tag = models.IntegerField(choices=address_tag_choices, verbose_name="地址标签", null=False, default=1)
    default_choices = [
        (1, "是"),
        (2, "否"),
    ]
    is_default = models.IntegerField(verbose_name="是否为默认收获地址", choices=default_choices, default=2, null=True)

    class Meta:
        db_table = "shipping_address"
        verbose_name = "收货地址表"

    def __str__(self):
        return self.nickname + "-" + self.address


# 订单表
class GoodsOrder(models.Model):
    user = models.ForeignKey(to="User", to_field="id", on_delete=models.DO_NOTHING, verbose_name="用户", null=False)
    order_name = models.CharField(max_length=128, verbose_name="订单号(唯一)", db_index=True, unique=True, null=False)
    goods_info = models.TextField(verbose_name="商品信息", null=False)
    price = models.DecimalField(verbose_name="订单支付金额", null=False, max_digits=5, decimal_places=2)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="订单生成时间")
    pay_time = models.DateTimeField(null=True, verbose_name="支付时间")
    pay_type_choices = [
        (1, "零钱"),
        (2, "支付宝"),
        (3, "微信"),
        (4, "银行卡"),
    ]
    pay_type = models.IntegerField(choices=pay_type_choices, verbose_name="支付方式", null=True)
    pay_state_choices = [
        (1, "未支付"),
        (2, "已支付"),
    ]
    pay_state = models.IntegerField(choices=pay_state_choices, verbose_name="支付状态", default=1, null=False)
    delete_choices = [
        (1, "是"),
        (1, "否")
    ]
    is_delete = models.IntegerField(choices=delete_choices, verbose_name="标识订单是否删除", default=2, null=False)

    class Meta:
        db_table = "goods_order"
        verbose_name = "商品订单"

    def __str__(self):
        return self.order_name + "-" + self.get_pay_state_display()


# 验证码
class VerifyCode(models.Model):
    phone = models.CharField(max_length=11, unique=True, null=False, db_index=True, verbose_name="手机号")
    picture_path = models.ImageField(verbose_name="图片存放地址", null=True)
    verify_code = models.CharField(max_length=32, verbose_name="验证码")
    update_time = models.DateTimeField(auto_now=True, verbose_name="最后一次更新时间")
    use_state_choices = [
        (1, "未使用"),
        (2, "使用"),
    ]
    # 是否被是否的标准，使用验证码通过验证
    is_use = models.IntegerField(choices=use_state_choices, default=1, verbose_name="验证码是否被使用")

    class Meta:
        db_table = "verify_code"
        verbose_name = "验证码表"

    def __str__(self):
        return self.phone + "-" + self.verify_code


# 购物车
class ShopCart(models.Model):
    user = models.ForeignKey(to="User", to_field="id", on_delete=models.DO_NOTHING)
    shop_cart = models.TextField(verbose_name="购物车信息", null=False)

    class Meta:
        db_table = "shop_cart"
        verbose_name = "购物车表"

    def __str__(self):
        return self.user.nickname + "的购物车"
